/**********************************************************
    COPYRIGHT
        KND, Inc. (c) 2011
    CLASS NAME
        platform_gpio.h
    DEPENDENCY

    HISTORY
		09/15/2011, created by Guangxing Liu
**********************************************************/
#include "platform_config.h"
#include "platform_gpio.h"
#include "platform_uart.h"

/************************************************************************
	FUNCTION NAME : platform_gpio_init
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		Platform gpio value init
************************************************************************/
void platform_gpio_init(void)
{

}

/************************************************************************
	FUNCTION NAME : platform_gpio_device_open
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		Gpio Device Open
************************************************************************/
DeviceHandle platform_gpio_device_open(void)
{
	platform_gpio_app_call* handle = (platform_gpio_app_call*)malloc(sizeof(platform_gpio_app_call));
	if(handle)
	{
		handle->iOpt = Platform_Gpio_App_Call_None;
	}
	return (DeviceHandle)handle;
}	

/************************************************************************
	FUNCTION NAME : platform_gpio_device_close
	INPUTS: 
		DeviceHandle handle
	OUTPUTS:	
		none
	DESCRIPTION:	
		Gpio Device Close
************************************************************************/
int platform_gpio_device_close(DeviceHandle handle)
{
	platform_gpio_app_call* me = (platform_gpio_app_call*)handle;
	if(me)
	{
		free(me);
	}
	return 0;
	
}/************************************************************************
	FUNCTION NAME : platform_gpio_device_ioctl
	INPUTS: 
		DeviceHandle handle, const char *params
	OUTPUTS:	
		none			  
	DESCRIPTION:	
		Gpio Device Control
************************************************************************/
int platform_gpio_device_ioctl(DeviceHandle handle, const char *params)
{
	platform_gpio_app_call* me = (platform_gpio_app_call*)handle;
	
	switch(*params)
	{
		case '0':
			me->iOpt = Platform_Gpio_App_Call_SetioDir;
			return 0;
		case '1':
			me->iOpt = Platform_Gpio_App_Call_SetioValue;
			return 0;
		case '2':
			me->iOpt = Platform_Gpio_App_Call_GetioValue;
			return 0;
		default:
			platform_printf("ERROR--platform_gpio_device_ioctl unkown params: %s\r\n",params);
			break;
	}

	return -1;
}

/************************************************************************
	FUNCTION NAME :platform_gpio_device_read
	INPUTS: 
		DeviceHandle handle, char *buf, int len
	OUTPUTS:	
		none
	DESCRIPTION:	
		Gpio Device Read
************************************************************************/
int platform_gpio_device_read(DeviceHandle handle, void *buf, int len)
{
	platform_gpio_app_call* me = (platform_gpio_app_call*)handle;
	
	if(!me)
		return -1;
	switch(me->iOpt)
	{
		case Platform_Gpio_App_Call_GetioValue:
			{
				platform_gpio_setvalue * pvalue = (platform_gpio_setvalue*)buf;

				volatile unsigned long pinvalue =GPIO_ReadValue(pvalue->gpio_num);

				pvalue->value = pinvalue & (1<<(pvalue->gpio.gpio_pin))? 1:0;

			}
	         return 0;
		default:
			platform_printf("ERROR--platform_gpio_device_read unkown params: %s\r\n",me->iOpt);
			break;
	}
	
	return -1;

}

/************************************************************************
	FUNCTION NAME : platform_gpio_device_write
	INPUTS: 
		DeviceHandle handle,  const char *buf, int len
	OUTPUTS:	
		none
	DESCRIPTION:	
		Gpio Device Write
************************************************************************/
int platform_gpio_device_write(DeviceHandle handle,  void *buf, int len)
{
	platform_gpio_app_call* me = (platform_gpio_app_call*)handle;
	if(!me || !buf)
		return -1;
	switch(me->iOpt)
	{
		case Platform_Gpio_App_Call_SetioDir:
			{
				platform_gpio_setdir * pdir = (platform_gpio_setdir*)buf;

				GPIO_SetDir(pdir->gpio_num,1<<(pdir->gpio.gpio_pin),pdir->dir);
			}
			return 0;
		case Platform_Gpio_App_Call_SetioValue:
			{
				platform_gpio_setvalue * pvalue = (platform_gpio_setvalue*)buf;

				GPIO_OutputValue(pvalue->gpio_num,1<<(pvalue->gpio.gpio_pin),pvalue->value);
			}
			return 0;
		default:
			platform_printf("ERROR--platform_gpio_device_write unkown params: %s\r\n",me->iOpt);
			break;
	}
	return -1;

}
/************************************************************************
	FUNCTION NAME : platform_init_gpio_device
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		Gpio Device Init
************************************************************************/
void platform_init_gpio_device(void)
{
	DeviceInterface gpio = {
                               platform_gpio_device_open,
                               platform_gpio_device_close,
                               platform_gpio_device_ioctl,
                               platform_gpio_device_read,
                               platform_gpio_device_write
                              };


	hyacinth_install_device(GPIO_DEVICE,&gpio);
}

/************************************************************************
	FUNCTION NAME : platform_finit_gpio_device
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		Gpio Device Finit
************************************************************************/
void platform_finit_gpio_device(void)
{
	hyacinth_uninstall_device(GPIO_DEVICE);
}









































